/**
 * 〈〉
 *
 * @author 王青杰
 * @create 2018-08-0810:20
 * @since 1.0.0
 */
package org.wlgzs.website.wlgzsoffice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wlgzs.website.wlgzsoffice.dao.DutyDao;
import org.wlgzs.website.wlgzsoffice.entity.TbDuty;
import org.wlgzs.website.wlgzsoffice.enums.ResultEnum;
import org.wlgzs.website.wlgzsoffice.service.DutyService;
import org.wlgzs.website.wlgzsoffice.util.ResultMsg;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DutyServiceImpl  implements DutyService{
    @Resource
    DutyDao dutyDao;

    @Override
    @Transactional
    public ResultMsg insert(TbDuty duty) {
        if(duty!=null){
            dutyDao.insert(duty);
            return new ResultMsg(ResultEnum.SAVE,duty);
        }
        return new ResultMsg(ResultEnum.UNSAVE,duty);
    }

    @Transactional
    @Override
    public ResultMsg delete(int dutyId) {
        if(dutyDao.selectByPrimaryKey(dutyId)!=null){
            dutyDao.deleteByPrimaryKey(dutyId);
            PageHelper.startPage(0,10);
            List<TbDuty> dutyList = dutyDao.checkAll();
            PageInfo pageInfo = new PageInfo(dutyList);
            return new ResultMsg(ResultEnum.DELETE,pageInfo);
        }
        PageHelper.startPage(0,10);
        List<TbDuty> dutyList = dutyDao.checkAll();
        PageInfo pageInfo = new PageInfo(dutyList);
        return new ResultMsg(ResultEnum.UNDELETE,pageInfo);

    }

    @Transactional
    @Override
    public ResultMsg update(TbDuty duty) {
        if(dutyDao.selectByPrimaryKey(duty.getDutyId())!=null){
            dutyDao.updateByPrimaryKeySelective(duty);
            return new ResultMsg(ResultEnum.UPDATE,duty);
        }
        return new ResultMsg(ResultEnum.UNUPDATE,duty);
    }

    @Transactional
    @Override
    public ResultMsg checkAll(int pageNum, TbDuty duty) {
        if(duty!=null) {
            if (duty.getDutyPersonnel() == null) {
                duty.setDutyPersonnel("");
            }
            if (duty.getDutyTime() == null) {
                duty.setDutyTime("");
            }
        }
        PageHelper.startPage(pageNum,10);
        List<TbDuty> dutyList = dutyDao.checkAll();
        //List<TbDuty> gradeLis t = dutyDao.checkAll();
        PageInfo pageInfo = new PageInfo(dutyList);
        return new ResultMsg(ResultEnum.QUIRY,pageInfo);
    }
}